/**
 * Created by Administrator on 2018/6/28.
 */
$(function () {
    var saveForm = $('#saveForm');
    $("#btn_submit").click(function () {
        submitForm();
    });
    $("#btn_resetting").click(function () {
        resetting();
    });
    $('.datetimepicker').datetimepicker({
        format: 'yyyy-mm-dd',
        autoclose: true,
        todayBtn: true,
        clearBtn:true,
        todayHighlight: true,
        showMeridian: true,
        pickerPosition:'bottom-left',
        language: 'zh-CN',//中文，需要引用zh-CN.js包
        startView: 2,//月视图
        minView: 4//日期时间选择器所能够提供的最精确的时间选择视图
    });

    saveForm.bootstrapValidator({
        message: 'This value is not valid',//通用提示语
        excluded: [':disabled', ':hidden', ':not(:visible)'],
        feedbackIcons: { //指定验证样式
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        live:'enabled',  // 生效规则（三选一）enabled 字段值有变化就触发验证 disabled,submitted 当点击提交时验证并展示错误信息
        fields: {
            name:{
                message: '姓名验证失败',
                validators: {
                    notEmpty: {
                        message: '姓名不能为空'
                    },
                }
            },
            phone:{
                message:'手机号验证失败',
                validators:{
                    notEmpty:{
                        message:'手机号不能为空'
                    },
                    stringLength:{
                        min:11,
                        max:11,
                        message: '手机号长度错误'
                    },
                    regexp:{
                        regexp:/^1[3|4|5|6|7|8|9][0-9]\d{4,8}$/,
                        message:'手机号格式错误！'
                    }
                }
            },
            repairType:{
                message:'维修类型验证失败',
                validators:{
                    notEmpty:{
                        message:'维修类型不能为空'
                    }
                }
            },
            deviceType:{
                message:'家电类别验证失败',
                validators:{
                    notEmpty:{
                        message:'加点类别不能为空'
                    }
                }
            },
            address:{
                message:'地址验证失败',
                validators:{
                    notEmpty:{
                        message:'地址不能为空'
                    }
                }
            }
        }
    })
});

/*提交表单*/
var submitForm = function () {
    var saveForm = $('#saveForm');
    saveForm .bootstrapValidator('validate');
    var flag = saveForm.data('bootstrapValidator').isValid();
    if(flag){
        var formData = saveForm.serialize();
        $.ajax({
            url:ctx+'/repair.action?method=saveRepair&_='+Math.random(),
            dataType:'json',
            type:'get',
            data:formData,
            success:function (data) {
                if(data!=null){
                    if(data.success=='success'){
                        layer.msg(data.msg,{icon:6,time:1*1000})
                    }else {
                        layer.msg(data.msg,{icon:5,time:2*1000})
                    }
                }
            }
        })
    }
};

var resetting = function () {
    $("#saveForm").find('*[name]').each(function () {
         $(this).val("");
    });
};